package com.hefu.virtuallife.ui.foods;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dz.utlis.ClassTools;
import com.dz.utlis.JavaUtils;
import com.dz.utlis.ScreenUtils;
import com.hefu.virtuallife.R;
import com.hefu.virtuallife.baseui.BaseActivity;
import com.hefu.virtuallife.nohttp.util.Toast;
import com.hefu.virtuallife.ui.foods.adapter.FoodPlanViewPageAdapter;
import com.hefu.virtuallife.ui.foods.adapter.PreSureFoodAdapter;
import com.hefu.virtuallife.ui.foods.bean.PlanFoodBean;
import com.hefu.virtuallife.utils.PopWindHelper;
import com.hefu.virtuallife.utils.Tools;
import com.hefu.virtuallife.widget.ToolGridView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * creat_user: zhengzaihong
 * Email:1096877329@qq.com
 * describe 预订界面
 **/
public class PreSureActivity extends BaseActivity implements PopWindHelper.PopclickListener {


    @BindView(R.id.iv_back)
    ImageButton ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.recycler_foods)
    RecyclerView recyclerFoods;
    @BindView(R.id.tv_sure)
    TextView tvSure;


    @BindView(R.id.et_edit_time)
    TextView etEditTime;
    @BindView(R.id.et_edit_address)
    TextView etEditAddress;

    @BindView(R.id.ll_times)
    LinearLayout llTimes;
    @BindView(R.id.ll_address)
    LinearLayout llAddress;

    private PreSureFoodAdapter presurefoodadapter;
    private PopWindHelper popwindhelper;

    private String[] times = new String[]{
            "8:29~8:38", "8:39~8:48", "8:49~8:58",
            "9:19~9:28", "9:29~9:38"
    };
    private String[] address = new String[]{
            "西安南里17号", "顺江小区23号"
    };
    private int typecheck = 0;

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_pre_sure_foods;
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        ivBack.setVisibility(View.VISIBLE);
        tvTitle.setVisibility(View.VISIBLE);
        tvTitle.setText("预订早餐");
        ivBack.setOnClickListener(v -> finish());
    }

    @Override
    protected void initData() {
        super.initData();

        presurefoodadapter = new PreSureFoodAdapter();
        recyclerFoods.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerFoods.setAdapter(presurefoodadapter);

        List<PlanFoodBean> listdata = new ArrayList<>();

        for (int i = 0; i < 4; i++) {
            PlanFoodBean planFoodBean = new PlanFoodBean();
            listdata.add(planFoodBean);
        }

        presurefoodadapter.replaceData(listdata);


        llTimes.setOnClickListener(view -> {
            typecheck = 0;
            showpop(llTimes, times);
        });
        llAddress.setOnClickListener(view -> {
            typecheck = 1;
            showpop(llAddress, address);
        });

        tvSure.setOnClickListener(view -> {
            Toast.show(mContext, "预订成功");
            ClassTools.toAct(mContext, MyMenueActivity.class);

        });
    }


    @Override
    public void callback(int position) {
        JavaUtils.outPrint("position:" + position);

        if (typecheck == 0) {
            etEditTime.setText(times[position]);
        } else {
            etEditAddress.setText(address[position]);
        }
    }


    public void showpop(LinearLayout ll, Object[] list) {
        if (null != popwindhelper) {
            popwindhelper.closepop();
        }
        popwindhelper = new PopWindHelper(this)
                .initPopAt(R.layout.poplistview, new int[]{(int) ScreenUtils.dip2px(mContext,200f),0})
                .bindClick(this)
                .setAdapterCallBackViewHolder((viewHolder, position) -> {
                            ((TextView) (viewHolder.view.findViewById(R.id.time_index))).setText((list[position].toString()));
                        }
                );

        popwindhelper.bindAdapter(popwindhelper.parentView().findViewById(R.id.lv_datalistview), Arrays.asList(list), R.layout.type_item);
        popwindhelper.show(ll);
    }


}
